<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../inc/js.jsp"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>用户管理</title>
    <jsp:include page="../inc/css.jsp"/>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <jsp:include page="../inc/menu.jsp"/>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card" style="margin-top: 10px">
                        <!-- /.card-header -->
                        <div class="card-header">
                            <button type="button" title="通知用户更新信息" class="btn btn-primary" onclick="_userList.fn.refreshUserInfo()">通知用户更新信息</button>

                            <c:if test="${sessionScope.is_localhost}">
                                <button type="button" title="同步原始数据" class="btn btn-info" onclick="_userList.fn.syncData()">同步原始数据</button>
                            </c:if>

                            <div class="form-inline" style="margin-top: 10px;">
                                <div class="form-group">
                                    <select id="searchCompanyList" class="form-control" style="margin-right: 10px;">
                                        <option value="">全部公司</option>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <input id="searchKeyWord" placeholder="关键词查询" class="form-control"/>
                                </div>

                                <button type="button" title="查询" class="btn btn-info" style="margin-left: 10px;" onclick="_userList.fn.reloadData()">查询</button>
                                <button type="button" title="重置搜索条件并查询" class="btn btn-success" style="margin-left: 10px;" onclick="_userList.fn.resetAndReloadData()">重置</button>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables" style="width: 99%">
                                    <colgroup>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>微信昵称</th>
                                        <th>备注名</th>
                                        <th>联系方式</th>
                                        <th>所属公司</th>
                                        <th style="max-width: 50px">头像</th>
                                        <th>备注</th>
                                        <th style="max-width: 160px">注册时间</th>
                                        <th style="width: 80px">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
</div>
<!-- ./wrapper -->

<div class="modal inModal fade" id="userModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">用户详情</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <form role="form" class="form-group">
                    <input type="hidden" id="userId"/>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">微信昵称：</label>

                        <div class="col-9">
                            <input type="text" id="userNickName" maxlength="30" class="form-control" style="width: 95%"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">备注名：</label>

                        <div class="col-9">
                            <input type="text" id="userName" maxlength="30" class="form-control" style="width: 95%"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">头像地址：</label>

                        <div class="col-9">
                            <input type="text" id="userAvatar" maxlength="200" class="form-control" style="width: 95%"/>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="_userList.fn.saveInfo()">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- page script -->
<script type="text/javascript">
    let _userList = {
        v: {
            list: [],
            dTable: null,
            pageNum: 0,
            companyId: 0,
            masterCompanyId: 0
        },
        fn: {
            init: function () {
                let masterCompanyId = Number($('#masterCompanyId').val());

                if (masterCompanyId > 0) {
                    _userList.v.masterCompanyId = masterCompanyId;

                    $('#searchCompanyList').css('display', 'none');
                } else {
                    _userList.fn.getCompanyList();
                }

                _userList.fn.dataTableInit();
            },
            getCompanyList: function () {
                $.post('common/companyList', {
                    id: 1
                }, function (result) {
                    if (result.length > 0) {
                        let content = '';
                        $.each(result, function (i, item) {
                            if (_userList.v.companyId === item.id) {
                                content += '<option value="' + item.id + '" selected>' + item.name + '</option>';
                            } else {
                                content += '<option value="' + item.id + '">' + item.name + '</option>';
                            }
                        });

                        $('#searchCompanyList').append(content);
                    } else {
                        info("暂无公司数据", "error");
                    }
                });
            },
            reloadData: function () {
                _userList.v.list = [];
                _userList.v.dTable.DataTable().ajax.reload(null, false);
            },
            resetAndReloadData: function () {
                $('#searchCompanyList').val('');
                $('#searchKeyWord').val('');

                _userList.fn.reloadData();
            },
            dataTableInit: function () {
                _userList.v.dTable = $tutu.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "autoWidth": true,//自动计算宽度
                    "lengthChange": false,//是否允许用户改变表格每页显示的记录数
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/user/list",
                        "type": "POST"
                    },
                    "language": $tutu.v.dataTableL,
                    "columns": [
                        {
                            "data": "nickName",
                            render: function (data) {
                                return formatStr(data, 4);
                            }
                        },
                        {
                            "data": "name",
                            render: function (data) {
                                return formatStr(data, 8);
                            }
                        },
                        {
                            "data": "mobile",
                            render: function (data) {
                                return formatStr(data, 11);
                            }
                        },
                        {
                            "data": "companyName",
                            render: function (data) {
                                return formatStr(data, 10);
                            }
                        },
                        {
                            "data": "avatar",
                            render: function (data) {
                                return formatPicUrl(data);
                            }
                        },
                        {
                            "data": "description",
                            render: function (data) {
                                return formatStr(data, 10);
                            }
                        },
                        {
                            "data": "createTime",
                            render: function (data) {
                                return crtTimeFtt(data);
                            }
                        },
                        {"data": null}
                    ],
                    /*添加小插件*/
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='数据列表' class='btn btn-info btn-sm childList'>" +
                                "<i class='fa fa-list'></i>" +
                                "</button>" +
                                "&nbsp;&nbsp;" +
                                "<button type='button' title='编辑' class='btn btn-primary btn-sm edit'>" +
                                "<i class='fa fa-edit'></i>" +
                                "</button>",
                            "targets": -1
                        }
                    ],
                    /*展示数据格式化*/
                    "createdRow": function (row, data, index) {
                        _userList.v.list.push(data);
                    },
                    stateSave: true, // 启用状态保存
                    stateSaveCallback: function (settings, data) {
                        localStorage.setItem('DataTables_' + settings.sInstance, JSON.stringify(data));
                    },
                    stateLoadCallback: function (settings) {
                        return JSON.parse(localStorage.getItem('DataTables_' + settings.sInstance));
                    },
                    /*回调函数*/
                    rowCallback: function (row, data) {
                        $('td', row).last().find(".childList").click(function () {
                            window.location.href = 'backend/user/child/index?id=' + data.id + '&pageNum=' + $tutu.currentPage;
                        });

                        $('td', row).last().find(".edit").click(function () {
                            _userList.fn.editInfo(data);
                        });
                    },
                    /*带参数*/
                    "fnServerParams": function (aoData) {
                        _userList.v.list = [];

                        if (_userList.v.masterCompanyId > 0) {
                            aoData.searchCompanyId = _userList.v.masterCompanyId;
                        } else {
                            aoData.searchCompanyId = $('#searchCompanyList').val();
                        }

                        aoData.keyWord = $('#searchKeyWord').val();
                    },
                    "fnDrawCallback": function (data) {
                        $tutu.uiForm(data);
                    }
                });
            },
            saveInfo: function () {
                let nickName = $('#userNickName').val();
                let name = $('#userName').val();
                let avatar = $('#userAvatar').val();

                if (nickName === '') {
                    info('请输入微信昵称', "error");
                    return;
                }

                if (avatar === '') {
                    info('请输入微信头像', "error");
                    return;
                }

                startLoading();

                $.post('backend/user/save', {
                    id: $('#userId').val(),
                    nickName: nickName,
                    name: name,
                    avatar: avatar
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("信息已保存", "success");

                        $("#userModal").modal("hide");

                        _userList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            editInfo: function (data) {
                $('#userId').val(data.id);
                $('#userNickName').val(data.nickName);
                $('#userName').val(data.name);
                $('#userAvatar').val(data.avatar);

                $("#userModal").modal("show");
            },
            refreshUserInfo: function () {
                $.post('backend/user/noticeRefreshUserInfo', {
                    id: 1
                }, function (result) {
                    info("通知已发送", "success");
                });
            },
            syncData: function () {
                $.post('backend/user/sync', {
                    id: 1
                }, function (result) {
                    if (result.code === 0) {
                        info("信息已同步", "success");
                    } else {
                        info(result.msg, "error");
                    }
                });
            }
        }
    };

    $(document).ready(function () {
        _userList.fn.init();

        window.onload = function () {
            checkSelectMenu("userLi");

            loadMenuColor('userLi');
        }
    });
</script>
</body>
</html>